//
//  JCMineHead.m
//  JiFengDaRen
//
//  Created by 陈如意 on 2018/5/7.
//  Copyright © 2018年 陈永发. All rights reserved.
//

#import "JCMineHead.h"
#import "UIImageView+WebCache.h"

@interface JCMineHead()

@property (nonatomic, strong) UIImageView * tapImage;

@property (nonatomic, strong) UIImageView * headImage;

@property (nonatomic, strong) UILabel * realName;

@property (nonatomic, strong) UILabel * phoneLabel;

@property (nonatomic, strong) UILabel * agentLabel;

@property (nonatomic, strong) UIButton * bankBtn;

@property (nonatomic, strong) UIButton * realNameBtn;


@end

@implementation JCMineHead

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self initUI];
    }
    return self;
}

- (void)initUI{
    
    self.tapImage = [UIImageView new];
    self.tapImage.image = [UIImage imageNamed:@"my_top"];
    self.tapImage.userInteractionEnabled = YES;
    [self addSubview:self.tapImage];
    
    self.headImage = [UIImageView new];
    self.headImage.layer.masksToBounds = YES;
    self.headImage.layer.cornerRadius = __I6Width(50)/2;
    [self.tapImage addSubview:self.headImage];
    
    self.realName = [UILabel new];
    self.realName.font = KSHSystemFont(20);
    self.realName.textColor = [UIColor whiteColor];
    self.realName.textAlignment = NSTextAlignmentCenter;
    self.realName.userInteractionEnabled = YES;
    UITapGestureRecognizer * tapGesturRecognizer =[[UITapGestureRecognizer alloc ]initWithTarget:self action:@selector(tapAction)];
    [self.realName addGestureRecognizer:tapGesturRecognizer];
    [self.tapImage addSubview:self.realName];
    
    self.phoneLabel = [UILabel new];
    self.phoneLabel.font = KSHSystemFont(12);
    self.phoneLabel.textColor = [UIColor whiteColor];
    [self.tapImage addSubview:self.phoneLabel];
    
    self.agentLabel = [UILabel new];
    self.agentLabel.font = KSHSystemFont(16);
    self.agentLabel.textAlignment = NSTextAlignmentCenter;
    self.agentLabel.textColor = [UIColor colorWithHexString:@"#EFAF52"];
    self.agentLabel.layer.masksToBounds = YES;
    self.agentLabel.layer.cornerRadius = __I6Height(25)/2;
    self.agentLabel.layer.borderWidth = 1;
    self.agentLabel.layer.borderColor = [UIColor colorWithHexString:@"#EFAF52"].CGColor;
    [self.tapImage addSubview:self.agentLabel];
    
    self.realNameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.realNameBtn setImage:[UIImage imageNamed:@"my_real_name"] forState:UIControlStateNormal];
    self.realNameBtn.tag = 1000;
    [self.realNameBtn addTarget:self action:@selector(myHeaderAction:) forControlEvents:UIControlEventTouchUpInside
     ];
    [self addSubview:self.realNameBtn];
    
    self.bankBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.bankBtn setImage:[UIImage imageNamed:@"my_bank_card"] forState:UIControlStateNormal];
    self.bankBtn.tag = 1001;
    [self.bankBtn addTarget:self action:@selector(myHeaderAction:) forControlEvents:UIControlEventTouchUpInside
     ];
    [self addSubview:self.bankBtn];
    

    
}
    
- (void)layoutSubviews{
    [super layoutSubviews];
    
    [self.tapImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
    }];
    
    [self.headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(__I6Height(40));
        make.width.height.mas_offset(__I6Width(50));
        make.centerX.equalTo(self);
    }];
    
    [self.realName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.headImage.mas_bottom).offset(__I6Height(20));
        make.width.mas_offset(200);
    }];

    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.realName.mas_bottom).offset(8);
    }];
    
    [self.agentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.phoneLabel.mas_bottom).offset(8);
        make.width.mas_offset(__I6Width(75));
        make.height.mas_offset(__I6Height(25));
    }];
    
    [self.bankBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tapImage.mas_bottom).offset(__I6Height(36));
        make.right.equalTo(self).offset(-__I6Width(80));
        make.bottom.equalTo(self).offset(-__I6Height(30));
    }];
    
    [self.realNameBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tapImage.mas_bottom).offset(__I6Height(36));
        make.left.equalTo(self).offset(__I6Width(80));
        make.bottom.equalTo(self).offset(-__I6Height(30));
    }];


}

- (void)setUserModel:(UserModel *)userModel{
    _userModel = userModel;    
    if ([userModel.avatar isEqualToString:@""]) {
        self.headImage.image = [UIImage imageNamed:@"my_head"];

    }else{
        [self.headImage sd_setImageWithURL:[NSURL URLWithString:userModel.avatar] placeholderImage:[UIImage imageNamed:@"my_head"]];
    }
    self.realName.text = userModel.nickname;
    self.phoneLabel.text = [Tool numberSuitScanf:userModel.phone];
    
    self.agentLabel.text = userModel.user_grade;

    if (userModel.has_certification == YES) {
        self.realNameBtn.selected = YES;
        self.bankBtn.selected = YES;
        [self.realNameBtn setImage:[UIImage imageNamed:@"my_real_name_already"] forState:UIControlStateNormal];
    }else{
        self.realNameBtn.selected = NO;
        self.bankBtn.selected = NO;
        [self.realNameBtn setImage:[UIImage imageNamed:@"my_real_name"] forState:UIControlStateNormal];
    }
}

- (void)myHeaderAction:(UIButton *)btn{
    
    if ([_delegate respondsToSelector:@selector(didMineViewBtnAction:)]) {
        [_delegate didMineViewBtnAction:btn];
    }
}

- (void)tapAction{
    
    if ([_delegate respondsToSelector:@selector(didUserNameAction)]) {
        [_delegate didUserNameAction];
    }
}

@end
